<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	  xmlns:th="http://www.thymeleaf.org">
	<head>
		<div th:replace="commons/html-header"></div>
	</head>
	<body class="skin-1">

	<div th:replace="commons/head"></div>



	<div class="main-container" id="main-container">

		<script type="text/javascript">

			try{gds.settings.check('main-container' , 'fixed')}catch(e){}

		</script>

		<div class="main-container-inner">

			<a class="menu-toggler" id="menu-toggler" href="#">

				<span class="menu-text"></span>

			</a>



			<div th:replace="admin/admin-menu"></div>

			<div class="main-content">

				<div class="breadcrumbs" id="breadcrumbs">

					<script type="text/javascript">

						try{gds.settings.check('breadcrumbs' , 'fixed')}catch(e){}

					</script>



					<ul class="breadcrumb">
						<li>
							<i class="icon-home home-icon"></i>
							<a href="#">Home</a>
						</li>
						<li class="active">
							<a href="/senstive">敏感项管理</a>
						</li>
					</ul><!-- .breadcrumb -->

				</div>
				<div class="page-content">

					<div class="page-header">
						<form class="form-inline" role="form">
							<div class="form-group">
								<label class="sr-only" for="c_DS_ITEM_NAME">安全项名称</label>
								<input type="text" class="form-control" id="c_DS_ITEM_NAME" placeholder="请输入安全项名称" />
							</div>
							<button type="button" id="btn-search" class="btn btn-sm btn-info"><i class="icon-search"></i>查询</button>
							<button type="button" id="btn-add" class="btn btn-sm btn-primary pull-right"><i class="icon-plus"></i>新增安全项</button>
						</form>

					</div>

					<div class="row">

						<div class="col-xs-12">
							<div class="table-responsive">

								<table id="dsItemTable" class="table table-striped table-bordered table-hover">
									<thead>
									<tr>

										<th class="center col-sm-1">序号</th>
										<th class="col-sm-2">安全项名称</th>
										<th>说明</th>
										<th>
										</th>
									</tr>
									</thead>
									<tbody>

									</tbody>
								</table>
							</div><!-- /.table-responsive -->
						</div><!-- /.col -->

					</div><!-- /.row -->

				</div><!-- /.page-content -->

			</div><!-- /.main-content -->

		</div><!-- /.main-container-inner -->

		<div class="modal fade" id="edit_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
			<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
							&times;
						</button>
						<h4 class="modal-title" id="myModalLabel">
						</h4>
					</div>
					<div class="modal-body">
						<form name="edit_form" action="/sensitive/save" method="post" class="form-horizontal">
							<input class="form-control" name="c_DS_ITEM_ID" autocomplete="off" type="hidden" id="md_C_DS_ITEM_ID" />
							<div class="form-group">
								<label class="col-sm-3 control-label" for="md_C_DS_ITEM_NAME">安全项名称</label>
								<div class="col-sm-9">
									<input class="form-control" name="c_DS_ITEM_NAME" type="text" id="md_C_DS_ITEM_NAME" placeholder="请输入安全项名称" />
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label" for="md_C_DS_ITEM_REMARK">安全项描述</label>
								<div class="col-sm-9">
									<textarea style="resize: none;" rows="6" class="form-control" type="text" name="c_DS_ITEM_REMARK" id="md_C_DS_ITEM_REMARK" placeholder="请输入安全项描述"/>
								</div>
							</div>
						</form>
					</div>
					<div class="modal-footer">
						<button type="button" class="btn btn-sm btn-default" data-dismiss="modal">取消
						</button>
						<button type="button" id="btn-save" class="btn btn-sm btn-primary">
							保存
						</button>
					</div>
				</div><!-- /.modal-content -->
			</div><!-- /.modal -->
		</div>

		<div class="modal fade" id="cfg_modal" tabindex="-1" role="dialog" aria-labelledby="cfgModalLabel" aria-hidden="true">
			<div class="modal-dialog modal-lg">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
							&times;
						</button>
						<h4 class="modal-title" id="cfgModalLabel">
							配置安全项元数据 &gt;&gt; <span id="cfgModal_ItemName"></span>
						</h4>
					</div>
					<div class="modal-body" style="min-height: 350px;max-height: 800px;">
						<div class="col-md-12">
							<div class="col-md-4">
								<h4>接口列表</h4>
								<table class="table table-striped">
									<thead>
									<tr>
										<th>序号</th>
										<th colspan="2">接口名称</th>
										<th colspan="2">URL</th>
									</tr>
									</thead>
									<tbody id="interfsBody">

									</tbody>
								</table>
							</div>
							<div class="col-md-8">
								<h5>接口返回信息</h5>
								<table class="table table-striped">
									<thead>
									<tr>
										<th></th>
										<th colspan="3">接口参数</th>
										<th colspan="3">参数名称</th>
										<th colspan="3">替换规则</th>
									</tr>
									</thead>
									<tbody id="iparamsBody">

									</tbody>
								</table>
							</div>
						</div>
					</div>
					<div class="modal-footer">
						<button type="button" class="btn btn-sm btn-default" data-dismiss="modal">取消</button>
						<button type="button" id="btn-save-cfg" class="btn btn-sm btn-primary">保存</button>
					</div>
				</div><!-- /.modal-content -->
			</div><!-- /.modal -->
		</div>

		<a href="#" id="btn-scroll-up" class="btn-scroll-up btn btn-sm btn-inverse">

			<i class="icon-double-angle-up icon-only bigger-110"></i>
		</a>

	</div><!-- /.main-container -->



	<!-- basic scripts -->
	<div th:replace="commons/html-js"></div>

	<!-- inline scripts related to this page -->

	<script>
		var c_DS_ITEM_NAME;
		var dataList;
		var ds_iparams_list = new Array();
		var curr_DS_ITEM_ID;
		var lastTD =
			'<td>' +
				'<div class="visible-md visible-lg hidden-sm hidden-xs btn-group"> ' +
					'<button type="button" class="btn btn-xs btn-warning" onclick="cfgItem(\'{{0}}\',\'{{1}}\')" > ' +
						'<i class="icon-flag bigger-120"></i>配置元数据 ' +
					'</button>' +
					'<button type="button" class="btn btn-xs btn-info" onclick="updateItem(\'{{0}}\')" >' +
						'<i class="icon-edit bigger-120"></i>修改 ' +
					'</button> ' +
					'<button type="button" class="btn btn-xs btn-danger" onclick="delItem(\'{{0}}\')" > ' +
						'<i class="icon-trash bigger-120"></i>删除 ' +
					'</button> ' +
				'</div> ' +
			'</td>';
		queryForList = function() {
		    $.post(contextPath + "/sensitive/list" , {c_DS_ITEM_NAME : c_DS_ITEM_NAME} , function (data) {
		        var html = '';
		        //<![CDATA[
				if (null != data && data.length > 0) {
                    dataList = data;
				   for (var i = 0; i<data.length; i ++) {
                       html += '<tr>';
				       html += ('<td class="center">' + (i+1) + '</td><td><div title="' + data[i].c_DS_ITEM_NAME + '">' + data[i].c_DS_ITEM_NAME + '</div></td><td><div title="' + (null == data[i].c_DS_ITEM_REMARK ? '' : data[i].c_DS_ITEM_REMARK) + '">' + (null == data[i].c_DS_ITEM_REMARK ? '' : data[i].c_DS_ITEM_REMARK) + '</div></td>');
				       html += lastTD.replace(/\{\{0\}\}/gi,data[i].c_DS_ITEM_ID).replace(/\{\{1\}\}/gi,data[i].c_DS_ITEM_NAME);
                       html += '</tr>';
				   }

				}
				// ]]>
				$("#dsItemTable tbody").html(html);
            })
		}
		initEditModal = function (title,id,name,remark) {
            $("#md_C_DS_ITEM_ID").val(id);
            $("#md_C_DS_ITEM_NAME").val(name);
            $("#md_C_DS_ITEM_REMARK").val(remark);
            $("#myModalLabel").text(title);
            $("#edit_modal").modal();
        }
        updateItem = function (c_DS_ITEM_ID) {
			var itemName;
			var itemRemark;
			// <![CDATA[
			if (null != dataList && dataList.length > 0)
			$(dataList).each(function(index, obj) {
			    if (obj.c_DS_ITEM_ID == c_DS_ITEM_ID) {
			        itemName = obj.c_DS_ITEM_NAME;
			        itemRemark = obj.c_DS_ITEM_REMARK;
				}
			});
			if (null != itemName ) {
			    this.initEditModal("修改安全项",c_DS_ITEM_ID,itemName, itemRemark);
			}
			// ]]>
        }
        delItem = function (c_DS_ITEM_ID) {
//		    <![CDATA[
			bootbox.confirm({
			    buttons: {
			      cancel: {
			          label : '取消',
					  className : 'btn btn-sm btn-default'
				  },
					confirm : {
			          	label : '确定',
						className: 'btn btn-sm btn-primary'
			      }
				},
                title : "<i class='icon-question-sign'></i> 确认",
			    message :'确定删除安全项？',
                size: 'small',
				callback : function (result) {
			        if (result) {
                        $.post(contextPath + "/sensitive/" + c_DS_ITEM_ID + "/delete" , function (data) {
                            var ok = false;
                            if (null != data) {
                                if (0 == data.code) {
                                    queryForList();
                                    ok = true;
                                }
                            }
                            if (!ok) {
                                bootbox.dialog({
                                    message: '<span class="bigger-110">删除失败</span>',
                                    buttons:
                                        {
                                            "danger" :
                                                {
                                                    "label" : "确定",
                                                    "className" : "btn-sm btn-danger"
                                                }
                                        }
                                });
                            }
                        });
					}
                }
			})
//            ]]>
        }
        cfgItem = function (c_DS_ITEM_ID,c_DS_ITEM_NAME) {
            ds_iparams_list = new Array();
            curr_DS_ITEM_ID = c_DS_ITEM_ID;
            $("#cfgModal_ItemName").text(c_DS_ITEM_NAME);
            $.post(contextPath + "/interf/list", function (data) {
                var html = '';
                // <![CDATA[
				if (null != data && data.length > 0) {
				    for (var i = 0; i< data.length; i ++) {
				    	html += '<tr onclick="showInterfParams(\'' + c_DS_ITEM_ID + '\',\'' + data[i].c_INTERF_ID + '\')"><td>' + (i+1) + '</td><td colspan="2"><div title="' + data[i].c_INTERF_NAME + '">' + data[i].c_INTERF_NAME + '</div></td><td colspan="2"><div title="' + data[i].c_INTERF_METHOD_NAME + '">' + data[i].c_INTERF_METHOD_NAME + '</div></td></tr>';
                    }
				}
				// ]]>
				$("#interfsBody").html(html);
                $("#iparamsBody").html('');
            } )
			$("#cfg_modal").modal();

        };
        showInterfParams = function (C_DS_ITEM_ID ,c_INTERF_ID) {
            // <![CDATA[
			var html = "";
				if (
				    null != ds_iparams_list
					&& null != ds_iparams_list[c_INTERF_ID]
					&& ds_iparams_list[c_INTERF_ID].length > 0) {
					for (var i = 0; i< ds_iparams_list[c_INTERF_ID].length; i++) {
					    var data = ds_iparams_list[c_INTERF_ID];
						html += '<tr><td><input onclick="checkedDsIparams(\'' + data[i].c_INTERF_ID + '\',\'' + data[i].c_IPARAMS_ID + '\',this)" type="checkbox" value="' + data[i].c_IPARAMS_ID + '" ';
						if (1 == data[i].ifChecked) {
							html += 'checked="checked"';
						}
                        var iparams = '';
                        if (data[i].c_IPARAMS_REMARK != null){
                            iparams = data[i].c_IPARAMS_REMARK;
                        }
						html += ' /></td><td colspan="3"><div title="' + data[i].c_IPARAMS_NAME + '">' + data[i].c_IPARAMS_NAME + '</div></td><td colspan="3"><div title="' + iparams + '">' + iparams + '</div></td><td colspan="3"><input type="text" class="width-95" name="c_IPARAMS_SENS" onblur="updateIParamSens(\'' + data[i].c_INTERF_ID + '\',\'' + data[i].c_IPARAMS_ID + '\',this)" value="' + (null == data[i].c_IPARAMS_SENS ? '' : data[i].c_IPARAMS_SENS) + '" /></td></tr>';
					}
                    $("#iparamsBody").html(html);
				} else {
                    $.post(contextPath + "/sensitive/showIParams" , {c_DS_ITEM_ID: C_DS_ITEM_ID,c_INTERF_ID : c_INTERF_ID} , function (data) {
                        var html = '';
                        if (null != data && data.length > 0) {
                            ds_iparams_list[c_INTERF_ID] = data;

                            for (var i = 0; i< data.length; i++) {
                                ds_iparams_list[c_INTERF_ID][i].c_DS_ITEM_ID = C_DS_ITEM_ID;
                                html +='<tr><td><input onclick="checkedDsIparams(\'' + data[i].c_INTERF_ID + '\',\'' + data[i].c_IPARAMS_ID + '\',this)" type="checkbox" value="' + data[i].c_IPARAMS_ID + '" ';
                                if (1 == data[i].ifChecked) {
                                    html += 'checked="checked"';
                                }
                                var iparams = '';
                                if (data[i].c_IPARAMS_REMARK != null){
                                    iparams = data[i].c_IPARAMS_REMARK;
                                }
                                html +=' /></td><td colspan="3"><div title="' + data[i].c_IPARAMS_NAME + '">' + data[i].c_IPARAMS_NAME + '</div></td><td colspan="3"><div title="' + iparams + '">' + iparams + '</div></td><td colspan="3"><input type="text" class="width-95" name="c_IPARAMS_SENS" onblur="updateIParamSens(\'' + data[i].c_INTERF_ID + '\',\'' + data[i].c_IPARAMS_ID + '\',this)" value="' + (null == data[i].c_IPARAMS_SENS ? '' : data[i].c_IPARAMS_SENS) + '" /></td></tr>'
                            }
                        }
                        $("#iparamsBody").html(html);
                    })
				}

			// ]]>

        };
        updateIParamSens = function (c_INTERF_ID, c_IPARAMS_ID, ipt) {
			$(ds_iparams_list[c_INTERF_ID]).each(function (index, obj) {
			    // <![CDATA[
				if (obj.c_INTERF_ID == c_INTERF_ID && obj.c_IPARAMS_ID == c_IPARAMS_ID) {
				    obj.c_IPARAMS_SENS = $(ipt).val();
				    return false;
				}
                // ]]>
            })
        };
        checkedDsIparams = function (c_INTERF_ID, c_IPARAMS_ID, ipt) {
            var isChecked = 0;
            if ($(ipt).prop("checked")) {
                isChecked = 1;
			}
            // <![CDATA[
			for (var  i = 0; i< ds_iparams_list[c_INTERF_ID].length; i ++) {
                if (ds_iparams_list[c_INTERF_ID][i].c_IPARAMS_ID == c_IPARAMS_ID) {
                    ds_iparams_list[c_INTERF_ID][i].ifChecked = isChecked;
                    break;
				}
			}
            // ]]>
        };
		$(document).ready(function () {
		    queryForList(null);
			$("#btn-search").on("click" , function () {
                c_DS_ITEM_NAME = $("#c_DS_ITEM_NAME").val();
				queryForList();
            });
			
			$("#btn-add").on('click' , function () {
                initEditModal("新增安全项",'','','');
            });

            $("#btn-save").on("click",function () {
                $.post(contextPath + '/sensitive/save',$("form[name=edit_form]").serializeArray(), function (data) {
                        //<![CDATA[
                        if (null != data) {
                            if (0 == data.code) {
                                $("#edit_modal").modal('hide');
                                queryForList()
                            } else if ( 500 == data.code) {
                                bootbox.dialog({
                                    message: '<span class="bigger-110">' + data.msg + '</span>',
                                    buttons:
                                        {
                                            "danger" :
                                                {
                                                    "label" : "确定",
                                                    "className" : "btn-sm btn-danger"
                                                }
                                        }
                                });
							}
                        }
                        return false;
                        // ]]>
                    });
                return false;
            });

            $("#btn-save-cfg").on("click" , function () {
                var arr = new Array;
                // <![CDATA[
                if (null != ds_iparams_list) {
                    for (var p in ds_iparams_list) {
                       if (null != ds_iparams_list[p] && ds_iparams_list[p].length > 0) {
                           for( var k = 0; k< ds_iparams_list[p].length; k ++) {
                              // if (ds_iparams_list[p][k].ifChecked == 1) {
                                   arr[arr.length] = ds_iparams_list[p][k];
                               //}
						   }
					   }
					}
				}
				if (null != arr && arr.length > 0) {

                    $.ajax({
                        type: "post",
                        url: contextPath + '/sensitive/saveCfgs',
                        data:JSON.stringify({c_DS_ITEM_ID:curr_DS_ITEM_ID, dsiInterParams:arr}),
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (data) {
                            if (null != data) {
                                bootbox.alert({
                                    buttons: {
                                        ok: {
                                            label: '确定',
                                            className: 'btn-sm btn-primary'
                                        }
                                    },
                                    title :"提示",
                                    message :data.msg,
                                    size: 'small',
                                    callback : function () {
                                        if (data.code == '0'){
                                            $("#cfg_modal").modal('hide');
                                            queryForList();
                                        }
                                    }
                                });
                            }
                            return false;
                        }, error: function () {
                            bootbox.dialog({
                                message: '<span class="bigger-110">配置元数据项出错！</span>',
                                buttons:
                                    {
                                        "danger" :
                                            {
                                                "label" : "确定",
                                                "className" : "btn-sm btn-danger"
                                            }
                                    }
                            });
                        }
                    })
				}
				// ]]>
            });

        });
	</script>
</body>

</html>

